<template>
	<view>
		<view>
			<view class="list-cell b-b m-t" style="margin-top: 0;"
			<!-- #ifndef MP -->
			 @click="chooseImageTo" 
			<!-- #endif -->
			
			 hover-class="cell-hover" :hover-stay-time="50">
			    <!-- #ifdef MP -->
				<button style="height: 80px;width: 80px;border-radius: 50%;padding: 0" open-type="chooseAvatar" @chooseavatar="chooseAvatar">
				<!-- #endif -->
				<image class="avatar-image" v-if="userInfo" :src="userInfo.user_info.avatar||'https://httzx.shanerxiang.com/uploads/missing-face.png'"></image>
				<image class="avatar-image" v-else src="https://httzx.shanerxiang.com/uploads/missing-face.png"></image>
				<!-- #ifdef MP -->
				</button>
				<!-- #endif -->
				
				<!-- <text class="cell-more yticon icon-you" style="flex: 1;text-align: right;margin: auto;"></text> -->
			</view>
			<view class="list-cell b-b m-t"  hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">昵称</text>
				<input type="nickname"  v-model="userInfo.user_info.nickname" maxlength="18" class="cell-input"/>
				
			</view>
			
		</view>
		<view class="list-cell log-out-btn" @click="saveUserInfo()" >
			<text class="cell-tit">保存</text>
		</view>
	</view>
</template>

<script>
	import {  
	    mapState
	} from 'vuex';  
	import store from '@/store/index';
	import UploadApi from '@/common/upload';
	import Api from '@/common/api';
	import {mapMutations} from 'vuex';
	export default {
		data() {
			return {
				modify_avatar:0
			};
		},
		computed:{
			...mapState(['userInfo','hasLogin']),
		},
		onLoad(){
			this.loadUser();
		},
		methods:{
			...mapMutations(['login']),
			chooseImageTo(){
				let that = this;
				uni.chooseImage({
					count: 1, //最多可以选择的图片张数，默认9
					sizeType: ['original'], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ['album'], //album 从相册选图，camera 使用相机，默认二者都有
					extension:['jpg','png','jpeg'],
					success: async (res)=> {
						const images = res.tempFilePaths;
						that.saveStorage(images[0])
						that.$store.state.userInfo.user_info.avatar=images[0];
						that.modify_avatar=1;
						// let rel =await UploadApi.apiUploadCall('post',UploadApi.apiName.upload_images,images,{code:111},true);
						// if(rel && rel!==false){
						// 	store.state.userInfo.user_info.avatar = rel;
						// }
					}
				});
			},
			async saveUserInfo(){
				
				let params={nickname:store.state.userInfo.user_info.nickname};
				if(this.modify_avatar==1){//修改过头像
					let upRel = await UploadApi.apiUploadCall('post',UploadApi.apiName.upload_images,[uni.getStorageSync('avatar_temp')],{code:111},true);
					if(!upRel || upRel==false){
						this.$api.msg('上传头像失败');
						return false;
					}
					params.avatar = upRel;
				}
				let res = await Api.apiCall('post',Api.apiName.update_user,params)
				this.modify_avatar=0;
				uni.removeStorageSync('avatar_temp');
				if(res.code ==0){
					this.$api.msg(`保存成功`);
					this.login(store.state.userInfo)
				}
			},
			async chooseAvatar(e) {
			    let { avatarUrl }   = e.detail;
				this.saveStorage(avatarUrl);
				this.$store.state.userInfo.user_info.avatar=avatarUrl;
				this.modify_avatar=1;
				// let rel =await UploadApi.apiUploadCall('post',UploadApi.apiName.upload_images,[avatarUrl],{code:111},true);
				// if(rel && rel!==false){
				// 	store.state.userInfo.user_info.avatar = rel;
				// }
				
			  },
			  //吧图片的临时路径存到storage，点击保存再上传
			saveStorage(filePath){
				uni.setStorage({
					key:'avatar_temp',
					data:filePath
				})
			},
			async loadUser(){
				await  Api.apiCall('get',Api.apiName.user_main)
			},
			
		},
		beforeDestroy(){
			this.modify_avatar=0;
			uni.removeStorage({
				key:'avatar_temp'
			})
		},
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
	}
	.list-cell{
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: #5eba8f;
				text-align: center;
				margin-right: 0;
			}
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
	}
	.avatar-image{
		border-radius: 50%;
		height: 80px;
		width: 80px;
	}
	.cell-input{
		text-align: right;
		margin-right: 2px;
	}

</style>
